{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append('../utils')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from aoc import * # get_input, d4, d8, d4n\n",
    "from atcoder.math import * # crt, pow_mod, inv_mod, floor_sum\n",
    "from atcoder.convolution import * # convolution\n",
    "from atcoder.string import * # suffix_array, lcp_array, z_algorithm\n",
    "from collections import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "YEAR = 2021\n",
    "DAY = 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = '''6,10\n",
    "0,14\n",
    "9,10\n",
    "0,3\n",
    "10,4\n",
    "4,11\n",
    "6,0\n",
    "6,12\n",
    "4,1\n",
    "0,13\n",
    "10,12\n",
    "3,4\n",
    "3,0\n",
    "8,4\n",
    "1,10\n",
    "2,14\n",
    "8,10\n",
    "9,0\n",
    "\n",
    "fold along y=7\n",
    "fold along x=5'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "inp = get_input(YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def do_transform(inp):\n",
    "    return list(filter(lambda x: len(x) > 0, inp.split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = do_transform(sample)\n",
    "inp = do_transform(inp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part1(inp):\n",
    "    s = set()\n",
    "    for line in inp:\n",
    "        if line[0] == 'f':\n",
    "            ns = set()\n",
    "            m = int(line.split('=')[1])\n",
    "            if line[11] == 'x':\n",
    "                for x, y in s:\n",
    "                    if x < m:\n",
    "                        ns.add((x, y))\n",
    "                    else:\n",
    "                        ns.add((2 * m - x, y))\n",
    "            else:\n",
    "                for x, y in s:\n",
    "                    if y < m:\n",
    "                        ns.add((x, y))\n",
    "                    else:\n",
    "                        ns.add((x, 2 * m - y))\n",
    "            return len(ns)\n",
    "        else:\n",
    "            x, y = map(int, line.split(','))\n",
    "            s.add((x, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "684"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1_ans = part1(inp)\n",
    "part1_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part1_ans, YEAR, DAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part2(inp):\n",
    "    s = set()\n",
    "    for line in inp:\n",
    "        if line[0] == 'f':\n",
    "            ns = set()\n",
    "            m = int(line.split('=')[1])\n",
    "            if line[11] == 'x':\n",
    "                for x, y in s:\n",
    "                    if x < m:\n",
    "                        ns.add((x, y))\n",
    "                    else:\n",
    "                        ns.add((2 * m - x, y))\n",
    "            else:\n",
    "                for x, y in s:\n",
    "                    if y < m:\n",
    "                        ns.add((x, y))\n",
    "                    else:\n",
    "                        ns.add((x, 2 * m - y))\n",
    "            s = ns\n",
    "        else:\n",
    "            x, y = map(int, line.split(','))\n",
    "            s.add((x, y))\n",
    "    \n",
    "    max_x = max(x[0] for x in s)\n",
    "    max_y = max(x[1] for x in s)\n",
    "    \n",
    "    for i in range(max_y + 1):\n",
    "        line = ''.join('#' if (j, i) in s else '.' for j in range(max_x + 1))\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#####\n",
      "#...#\n",
      "#...#\n",
      "#...#\n",
      "#####\n"
     ]
    }
   ],
   "source": [
    "part2(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..##.###..####.###..#.....##..#..#.#..#\n",
      "...#.#..#....#.#..#.#....#..#.#.#..#..#\n",
      "...#.#..#...#..###..#....#....##...####\n",
      "...#.###...#...#..#.#....#.##.#.#..#..#\n",
      "#..#.#.#..#....#..#.#....#..#.#.#..#..#\n",
      ".##..#..#.####.###..####..###.#..#.#..#\n"
     ]
    }
   ],
   "source": [
    "part2(inp)"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
